{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "f85c710b",
   "metadata": {},
   "source": [
    "----\n",
    "* 日期： 2022-10-12\n",
    "* python"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d560bd10",
   "metadata": {},
   "source": [
    "# 本周学习内容\n",
    "> 1.有序的数据结构-字符串\n",
    ">>1.字符串的基本概念（回顾）\n",
    "\n",
    ">>2.字符串的性质和方法\n",
    "\n",
    "> 2.有序的数据结构-列表"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "661b4066",
   "metadata": {},
   "source": [
    "## 1.字符串的性质和方法\n",
    "* 字符串的性质\n",
    "> 1. 字符串的有序性\n",
    "> 2. 字符串是有长度的\n",
    ">> 1. 使用len()检查字符串的长度\n",
    "* 字符串的基础方法(运算法则)\n",
    "> 1. 字符串的取值: str[index]\n",
    "> 2. 字符串的切片: str[start,stop],stop取不到\n",
    "> 3. 字符串的拼接:+\n",
    "> 4. in 检查成员是否存在\n",
    "* 字符串的一般方法(function,函数/方法)\n",
    "> 1. isdigit()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "bb5c309f",
   "metadata": {},
   "outputs": [],
   "source": [
    "text=\"广州南方学院团委\"                   #values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "e302a66e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'广'"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "text[0]                                   #01234=index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "431583d4",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "ename": "IndexError",
     "evalue": "string index out of range",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mIndexError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-9-9db657f07349>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mtext\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m100\u001b[0m\u001b[1;33m]\u001b[0m                  \u001b[1;31m#index值超过\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mIndexError\u001b[0m: string index out of range"
     ]
    }
   ],
   "source": [
    " text[100]                  #index值超过"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "c962fbe5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "73b4b15f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'广州南'"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "text[0]+text[1]+text[2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "871693dc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'广州南方学院'"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "text[0:6]          # 等价于 [:6]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "64936abb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'州南方学院团委'"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "text[1:8]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "291d2320",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'广州南方学院团委'"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "text[:8]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "8694eadb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'院团'"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "text[-3:-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "e3898586",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'团委'"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "text[-2:]     #倒着取如果取最后一个字,则空着"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "58be8be4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'广州南方学院团委广州南方学院团委'"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "2*text"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b70c4bca",
   "metadata": {},
   "outputs": [],
   "source": [
    "* in 检查成员对象\n",
    "* .count()检查字符串出现的次数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "072ce68a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"广州\"in text"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "e6ddbb27",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"北京\"in text"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "cc5b34b3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "3*text.count(\"广州\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c82c00ef",
   "metadata": {},
   "source": [
    "### isdigit"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "5b336960",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "19\n",
      "19\n"
     ]
    }
   ],
   "source": [
    "print(19)\n",
    "print(\"19\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "0a57e04b",
   "metadata": {},
   "outputs": [],
   "source": [
    "age_1=19\n",
    "age_2=\"19\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d72e091d",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "53afad7c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "19 <class 'int'>\n",
      "19 <class 'str'>\n"
     ]
    }
   ],
   "source": [
    "print(age_1,type(age_1))\n",
    "print(age_2,type(age_2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "1f0b92b5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "38\n"
     ]
    }
   ],
   "source": [
    "# 如果字符串是数值,可以用int转换成真正的数值\n",
    "\n",
    "if age_2.isdigit():\n",
    "    print(age_1+int(age_2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "592b7f47",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "19\n",
      "19\n"
     ]
    }
   ],
   "source": [
    "print(age_1)\n",
    "print(age_2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "912cf7b0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "age_2.isdigit()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "f114ec7d",
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "unsupported operand type(s) for +: 'int' and 'str'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-49-a38ab5873c85>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mage_1\u001b[0m\u001b[1;33m+\u001b[0m\u001b[0mage_2\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m: unsupported operand type(s) for +: 'int' and 'str'"
     ]
    }
   ],
   "source": [
    "age_1+age_2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "e39e4cf4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "38"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "age_1+int(age_2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "24c984c5",
   "metadata": {},
   "source": [
    "## 2.有序的数据结构-列表(list)\n",
    "* 1. 列表的概念和基本性质\n",
    "> 1.Python knows a number of compound data types, used to group together other values. The most versatile is the list, which can be written as a list of comma-separated values (items) between square brackets. Lists might contain items of different types, but usually the items all have the same type.\n",
    "\n",
    "* 2. 列表的一般方法(如何学习)\n",
    "> 1. 使用.Tab查看列表的方法\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "dd7e6532",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'1'"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "text=\"3724173892\"\n",
    "text[4]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e46eb29b",
   "metadata": {},
   "source": [
    "## 2.1 列表的性质\n",
    "* 和字符串的运算法则完全一致\n",
    "* 注意:字符串是对字符进行操作,列表是对item元素进行操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "056ece5c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 4, 9, 16, 25]"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 列表的形式\n",
    "squares = [1, 4, 9, 16, 25]      #列表里面放数值   values\n",
    "          # 0   1  2   3  4          index\n",
    "squares"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "e32403a5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "25"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "squares[4]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "id": "b154f426",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "4 in squares"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "id": "6cd36ebd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "squares.count(9)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "id": "12f7cf9f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Jack', 'Alex', 'Marry', 'Jan']"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names=[\"Jack\",\"Alex\",\"Marry\",\"Jan\"] #列表里面放字符串\n",
    "names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "id": "515c3a63",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Jack', 'Alex', 'Marry', 'Jan', 1, 4, 9, 16, 25]"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names+squares"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "id": "60096451",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 4, 9, 16, 25, 1, 4, 9, 16, 25]"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "2*squares"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "id": "798ace98",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Alex', 'Marry']"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names[1:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "id": "0be2b39b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Marry', 'Jan']"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names[-2:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "id": "34f53b82",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Jack', 'Marry']"
      ]
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 取出\"Jack\",\"Marry\" 用切片的方法\n",
    "names[:3:2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1cd1ce11",
   "metadata": {},
   "outputs": [],
   "source": [
    "* 2. 列表的一般方法(如何学习)\n",
    "> 1.增加\n",
    "> 2. 删除\n",
    "> 3. 改\n",
    "> 3. 查\n",
    "* 列表的学习方法\n",
    "> 1. 使用.Tab查看列表的方法\n",
    "> 2. 使用help(查看一般方法的使用规则)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "id": "d46ee9cf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Jack', 'Alex', 'Marry', 'Jan']"
      ]
     },
     "execution_count": 78,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names=[\"Jack\",\"Alex\",\"Marry\",\"Jan\"] \n",
    "names                #append 增加          清除        复制         插入     增加        移除"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "id": "5c1d71d1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function append:\n",
      "\n",
      "append(object, /) method of builtins.list instance\n",
      "    Append object to the end of the list.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(names.append)         #append 增加  到列表的结尾"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "id": "01e11548",
   "metadata": {},
   "outputs": [],
   "source": [
    "names.append(\"Lihua\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "id": "e37de226",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Jack', 'Alex', 'Marry', 'Jan', 'Lihua']"
      ]
     },
     "execution_count": 82,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b1afac19",
   "metadata": {},
   "source": [
    "# 课后作业\n",
    "*列表的方法 尝试所有的方法\n",
    "*总结列表的方法学习心得(表格形式总结)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9e5e4a4b",
   "metadata": {},
   "source": [
    "| Syntax      | Description |\n",
    "| ----------- | ----------- |\n",
    "| Header      | Title       |\n",
    "| Paragraph   | Text        |"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5382453b",
   "metadata": {},
   "source": [
    "|列表的方法| 描述 |使用方法 | 备注 |\n",
    "| ---------| ----------- | ----------- | ----------- |\n",
    "| append  |Append object to the end of the list.|list.append(object)| 注意增加在末尾 |\n",
    "| count   |统计某个元素在列表中出现的次数 |list.count(object)|查找列表中的元素|\n",
    "|extend   |在列表末尾一次性追加另一个序列中的多个值|list.extend(seq)|用新列表扩展原来的列表|\n",
    "|index    |从列表中找出某个值第一个匹配项的索引位置|list.index（object）||\n",
    "|insert   |将对象插入列表|list.insert（）|默认往最后面插入这个对象|\n",
    "|clear    |removes all items from list|list.clear()|列表本身并不会被删除|\n",
    "|copy    |列表的拷贝|list.copy()||\n",
    "|pop|移除列表里面的不想要的元素,并返回该元素|list.pop()|如果不传入任何参数，那么默认就移除最后那个元素，并返回这个元素|\n",
    "|remove|传入你要移除的那个元素|list.remove()|不是那个元素的索引序号|\n",
    "|reverse|列表倒序|list.reserve|反向列表中元素|\n",
    "|sort|对列表进行排序|list.sort|对原列表进行排序|\n",
    "|mro|find a method in a class.|list.mro()||\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "07156b8f",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
